﻿package mortal.game.view.best
{
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.best.panel.*;
    import mortal.game.view.task.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class BestController extends Controller
    {
        private var isAllowShowWin:Boolean = false;
        private var isLoad:Boolean = false;
        private var _isAddEvs:Boolean = false;
        private var _isCreate:Boolean = false;
        private var _cle:int;
        private var _skillStar:Boolean = false;
        private var _skillUpdateTime:uint = 0;

        public function BestController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpgrade);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.allowShowWin);
            Dispatcher.addEventListener(EventName.BestTaskTrackModeOpen, this.isPanelCreate);
            return;
        }// end function

        private function addSerEvs() : void
        {
            Dispatcher.addEventListener(EventName.BestViewRefresh, this.bestViewRefresh);
            Dispatcher.addEventListener(EventName.BestViewShow, this.showView);
            EventUtil.addNetEvent(ServerCommand.MountLevelUp, this.updateMount, true);
            EventUtil.addNetEvent(ServerCommand.MountBlessUpdata, this.updateMount, true);
            EventUtil.addNetEvent(ServerCommand.FashionInfoUpdate, this.updateWPC, true);
            EventUtil.addSelfEvent(EventName.ForgingPerfectStrenSucc, this.updateEquip, true);
            EventUtil.addSelfEvent(EventName.ForgingEquipReplace, this.updateEquip, true);
            EventUtil.addNetEvent(ServerCommand.updateEquipMent, this.updateEquipAndGem, true);
            EventUtil.addSelfEvent(EventName.ForgingGemInfoUpdate, this.updateGem, true);
            EventUtil.addNetEvent(ServerCommand.BackPackItemsChange, this.packGemChangem, true);
            EventUtil.addNetEvent(ServerCommand.BackPackItemDel, this.packGemChangem, true);
            EventUtil.addNetEvent(ServerCommand.BackPackItemAdd, this.packGemChangem, true);
            EventUtil.addSelfEvent(EventName.ForgingGemUpgradeSucc, this.updateGem, true);
            EventUtil.addNetEvent(ServerCommand.WizardActive, this.updateWizards, true);
            EventUtil.addNetEvent(ServerCommand.WizardSkillUpdate, this.updateWizards, true);
            EventUtil.addNetEvent(ServerCommand.WizardAcupointLevelUp, this.updateWizards, true);
            EventUtil.addNetEvent(ServerCommand.BestSkillUpdate, this.updateSkill, true);
            EventUtil.addSelfEvent(EventName.BestSkillUpdate, this.updateSkill, true);
            EventUtil.addNetEvent(ServerCommand.PetAddOrRemove, this.updatePet, true);
            EventUtil.addNetEvent(ServerCommand.BestPetUpdate, this.updatePet, true);
            if (BestUtil.needShopInfo())
            {
                EventUtil.addNetEvent(ServerCommand.RebateShopGetSOnsellItemsMsg, this.onUpdateItemsNum, true);
                EventUtil.addNetEvent(ServerCommand.RebateShopGetSOnsellPlayerMsg, this.onUpdatePlayerRecord, true);
                EventUtil.addNetEvent(ServerCommand.BackPackItemsAdd, this.onUpdatePackage, true);
            }
            EventUtil.addSelfEvent(EventName.BestTextClick, this.clickTxt, true);
            EventUtil.addSelfEvent(EventName.MainCityTaxUpdate, this.upCityTax, true);
            EventUtil.addSelfEvent(EventName.ExpCopyEnterNumUpdate, this.upExpCopy, true);
            return;
        }// end function

        private function addWinEvents(param1:Boolean) : void
        {
            if (param1)
            {
                if (BestUtil.needShopInfo())
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BestGetRebatShopConfig));
                }
            }
            return;
        }// end function

        override protected function initView() : IView
        {
            return null;
        }// end function

        public function get infoPanel() : BestNewInfoPanel
        {
            return (GameController.task.view as TaskTrackModuleNew).bestInfoPanel;
        }// end function

        private function bestViewRefresh(event:DataEvent) : void
        {
            if (this.infoPanel && this.canUpdateInfo())
            {
                this.infoPanel.refresh();
            }
            return;
        }// end function

        private function showPanel(param1:Boolean) : void
        {
            if (this.infoPanel && this.canUpdateInfo())
            {
                this.infoPanel.visible = param1;
            }
            return;
        }// end function

        private function allowShowWin(event:Event) : void
        {
            this.isLoad = true;
            if (this._isCreate)
            {
                this.showView(null);
            }
            return;
        }// end function

        private function isPanelCreate(event:DataEvent) : void
        {
            this._isCreate = true;
            this._cle = setTimeout(this.deFre, 500);
            return;
        }// end function

        private function deFre() : void
        {
            clearTimeout(this._cle);
            this.showView(null);
            return;
        }// end function

        private function levelUpgrade(event:Event) : void
        {
            if (this.checkLevelEnougth())
            {
                if (this._isAddEvs == false)
                {
                    this._isAddEvs = true;
                    this.addSerEvs();
                }
                this.showView(null);
            }
            return;
        }// end function

        private function showView(event:DataEvent) : void
        {
            if (this.isLoad == false)
            {
                return;
            }
            if (this.checkLevelEnougth())
            {
                this.showPanel(true);
                this.bestViewRefresh(null);
                this.addWinEvents(true);
            }
            else
            {
                this.showPanel(false);
                this.addWinEvents(false);
            }
            return;
        }// end function

        private function checkLevelEnougth() : Boolean
        {
            var _loc_1:* = Cache.instance.role.roleEntityInfo.entityInfo.level;
            if (_loc_1 >= GameDefConfig.instance.getSystemOpenLevel(46))
            {
                return true;
            }
            return false;
        }// end function

        private function updateMount(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshMount();
            }
            return;
        }// end function

        private function updateWPC(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.updateWPC();
            }
            return;
        }// end function

        private function updateEquip(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshEquip();
            }
            return;
        }// end function

        private function updateEquipAndGem(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshEquip();
                this.infoPanel.freshGem();
            }
            return;
        }// end function

        private function packGemChangem(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshGem();
            }
            return;
        }// end function

        private function updateGem(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshGem();
            }
            return;
        }// end function

        private function updateWizards(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshWizards();
            }
            return;
        }// end function

        private function updateSkill(param1:Object) : void
        {
            if (this._skillStar)
            {
                return;
            }
            this._skillStar = true;
            this._skillUpdateTime = setTimeout(this.upSkillTime, 1 * 1000);
            return;
        }// end function

        private function upSkillTime() : void
        {
            this._skillStar = false;
            clearTimeout(this._skillUpdateTime);
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshSkill();
            }
            return;
        }// end function

        private function updatePet(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshPet();
            }
            return;
        }// end function

        private function onUpdateItemsNum(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshShop();
            }
            return;
        }// end function

        private function onUpdatePlayerRecord(param1:Object) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshShop();
            }
            return;
        }// end function

        private function onUpdatePackage(param1:Object) : void
        {
            var _loc_2:* = param1 as Array;
            if (_loc_2 && (UpdateCode.EUpdateCodeGift == _loc_2[1] || UpdateCode.EUpdateCodeShopPanicBuy == _loc_2[1]))
            {
                if (this.canUpdateInfo())
                {
                    this.infoPanel.freshShop();
                }
            }
            return;
        }// end function

        public function clickTxt(event:DataEvent) : void
        {
            if (this.canUpdateInfo())
            {
                BestUtil.clickTxt(event);
            }
            return;
        }// end function

        private function canUpdateInfo() : Boolean
        {
            if (this.infoPanel && this.infoPanel.isDisposed == false && this.checkLevelEnougth())
            {
                return true;
            }
            return false;
        }// end function

        private function upCityTax(param1:Object = null) : void
        {
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshCityTax();
            }
            return;
        }// end function

        private function upExpCopy(event:DataEvent) : void
        {
            BestUtil.expCopyTime = event.data as int;
            if (this.canUpdateInfo())
            {
                this.infoPanel.freshExpCopy();
            }
            return;
        }// end function

    }
}
